/*
 * DBeaver - Universal Database Manager
 * Copyright (C) 2010-2025 DBeaver Corp and others
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jkiss.dbeaver.model.sql.semantics.model.select;

import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.model.sql.semantics.SQLQueryRecognitionContext;
import org.jkiss.dbeaver.model.sql.semantics.SQLQuerySymbol;
import org.jkiss.dbeaver.model.sql.semantics.context.SQLQueryExprType;
import org.jkiss.dbeaver.model.sql.semantics.context.SQLQueryResultColumn;
import org.jkiss.dbeaver.model.sql.semantics.context.SQLQueryRowsDataContext;
import org.jkiss.dbeaver.model.sql.semantics.context.SQLQueryRowsSourceContext;
import org.jkiss.dbeaver.model.sql.semantics.model.SQLQueryNodeModelVisitor;
import org.jkiss.dbeaver.model.sql.semantics.model.expressions.SQLQueryValueExpression;
import org.jkiss.dbeaver.model.stm.STMTreeNode;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * Describes a table constructed by VALUES clause
 */
public class SQLQueryRowsTableValueModel extends SQLQueryRowsSourceModel {
    @NotNull
    private final List<List<SQLQueryValueExpression>> rows;
    private final boolean isIncomplete;
    
    public SQLQueryRowsTableValueModel(
        @NotNull STMTreeNode syntaxNode,
        @NotNull List<List<SQLQueryValueExpression>> rows,
        boolean isIncomplete
    ) {
        super(syntaxNode);
        this.rows = rows;
        this.isIncomplete = isIncomplete;
    }

    @NotNull
    public List<SQLQueryValueExpression> getValues() {
        return this.rows.isEmpty() ? Collections.emptyList() : this.rows.getFirst();
    }

    @Override
    protected SQLQueryRowsSourceContext resolveRowSourcesImpl(
        @NotNull SQLQueryRowsSourceContext context,
        @NotNull SQLQueryRecognitionContext statistics
    ) {
        return this.isIncomplete ? context.resetAsUnresolved() : context.reset();
    }

    @Override
    protected SQLQueryRowsDataContext resolveRowDataImpl(
        @NotNull SQLQueryRowsDataContext context,
        @NotNull SQLQueryRecognitionContext statistics
    ) {
        LinkedList<SQLQueryResultColumn> resultColumns = new LinkedList<>();
        SQLQueryRowsDataContext emptyTuple = this.getRowsSources().makeEmptyTuple();
        int rowIndex = 0;
        for (List<SQLQueryValueExpression> row : this.rows) {
            for (SQLQueryValueExpression value : row) {
                value.resolveRowSources(this.getRowsSources(), statistics);
                value.resolveValueRelations(emptyTuple, statistics);
                if (rowIndex == 0) {
                    resultColumns.addLast(
                        new SQLQueryResultColumn(
                            resultColumns.size(),
                            new SQLQuerySymbol("?"),
                            this,
                            null,
                            null,
                            SQLQueryExprType.UNKNOWN
                        )
                    );
                }
            }
            rowIndex++;
        }


        return this.getRowsSources().makeTuple(this, List.copyOf(resultColumns), Collections.emptyList());
    }

    @Override
    protected <R, T> R applyImpl(@NotNull SQLQueryNodeModelVisitor<T, R> visitor, @NotNull T arg) {
        return visitor.visitRowsTableValue(this, arg);
    }
}